package com.ruiyi.adrobot.web.controller.taskdetail;

import com.ruiyi.adrobot.beans.TaskDetail;
import com.ruiyi.adrobot.beans.TaskErrorCode;
import com.ruiyi.adrobot.beans.common.MongoPageRequest;
import com.ruiyi.adrobot.beans.common.ResultMsg;
import com.ruiyi.adrobot.mongo.robot.RobotRepository;
import com.ruiyi.adrobot.mongo.taskdetail.TaskDetailRepository;
import com.ruiyi.adrobot.service.taskdetail.TaskDetailService;
import com.ruiyi.adrobot.component.TaskExcuter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruiyi.adrobot.component.TaskExcuter.stopTaskList;

/**
 * Created by tao.zeng on 2017/5/17.
 */
@Controller
@RequestMapping("/taskDetail")
public class TaskDetailController {
    @Autowired
    private TaskDetailRepository taskDetailRepository;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private RobotRepository robotRepository;
    @Autowired
    private TaskExcuter taskExcuter;
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Integer> redisOperations;


    @RequestMapping(value = "/toList", method = RequestMethod.GET)
    public String toList() {
        return "taskdetail/list";
    }

    @GetMapping("/getListByPage")
    @ResponseBody
    public Page<TaskDetail> getListByPage(MongoPageRequest<Map<String, Object>> mongoPageRequest, @RequestParam("startTimeLevel") Integer startTimeLevel) {
        Map<String, Object> param = new HashMap<>();
        param.put("startTimeLevel", startTimeLevel);
        mongoPageRequest.setParams(param);
        return taskDetailRepository.getListByPage(mongoPageRequest);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg delete(String id) {
        taskDetailRepository.delete(id);
        return ResultMsg.success();
    }

    @RequestMapping(value = "/getLastTaskByFun", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg<TaskDetail> getLastTaskByFun(Integer fun) {
        TaskDetail taskDetail = taskDetailRepository.getLastTaskByFun(fun);
        return ResultMsg.success(taskDetail);
    }

    @RequestMapping(value = "/stopTask", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg<Map<String, Object>> stopTask(String id) {
        taskDetailService.stopTaskById(id);
        return ResultMsg.success();
    }

    @GetMapping("/recover/{id}/{excuted}")
    @ResponseBody
    public ResultMsg<Map<String, Object>> pause(@PathVariable("id") String id, @PathVariable("excuted") Boolean excuted) {
        stopTaskList.remove(id);
        Update update = new Update();
        update.set("finished", false);
        update.set("excuted", excuted);
        taskDetailRepository.updateById(id, update);
        return ResultMsg.success();
    }

    @RequestMapping(value = "/getVpnOnline", method = RequestMethod.GET)
    @ResponseBody
    public Integer getVpnOnline() {
        return Integer.valueOf(redisOperations.get("vpnOnline"));
    }

    @GetMapping("/toDetail/{id}/{fun}")
    public String toDetail(@PathVariable("id") String id, @PathVariable("fun") Integer fun, ModelMap model) {
        model.put("id", id);
        model.put("fun", fun);
        return "taskdetail/detail";
    }

    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    @ResponseBody
    public TaskDetail getDetail(String id) {
        return taskDetailRepository.findOne(id);
    }

    @GetMapping("/getErrorCodeByGroup/{id}/{fun}")
    @ResponseBody
    public List<TaskErrorCode> getErrorCodeByGroup(@PathVariable("id") String id, @PathVariable("fun") Integer fun) {
        return taskDetailRepository.getErrorCodeByGroup(id, fun);
    }
}


